<?php
/**
 * ThinkSNS Action控制器基类
 * @author  dongheng <liuxiaoqing@zhishisoft.com>
 * @version TS3.0
 */
abstract class Action
{//类定义开始
    
    // 当前Action名称
    private		$name =  '';
    //返回JSON格式
    public     $JSON = 'json';
    //返回HTML格式
    public     $HTML = 'html';
    //使用smarty模板技术
    public     $SMARTY = 'smarty';
    //返回javascript格式
    public     $JS = 'js';
    //返回xml格式
    public     $XML = 'xml';
    //模板引擎对象
    public $smarty = null;
    public     $eachPageCount = 30;
    protected   $tVar =  array();
    protected   $trace = array();
    
    public function init(){    	
        tsLoad('/core/CoreLib/libs/Smarty.class.php');
        $this->smarty = new Smarty();
        
        //$smarty->force_compile = true;
        $this->smarty->debugging = false;
        $this->smarty->caching = false;
        $this->smarty->cache_lifetime = 0;                 //300秒5分钟
        $this->smarty->template_dir = SITE_PATH.'/html/';    //html文件存放的位置
        $this->smarty->compile_dir = SITE_PATH.'/templates_c/';  //编译文件指定的目录
        
        $this->smarty->config_dir = SITE_PATH.'/configs/';  //配置文件指定的目录
        $this->smarty->cache_dir = SITE_PATH.'/cache/';  //缓存文件指定的目录
        $this->smarty->assign("charset", "utf-8");
    }
    
    public function getAllVar(){
        return $this->tVar;
    }
    
    public function getSmarty(){
    	return $this->smarty;
    }
	
    /**
     * 模板Title
     * @access public
     * @param mixed $input 要
     * @return
     */
    public function setTitle($title = '') {
		$this->assign('_title',$title);
	}

    /**
     * 模板keywords
     * @access public
     * @param mixed $input 要
     * @return
     */
    public function setKeywords($keywords = '') {
        $this->assign('_keywords',$keywords);
    }

    /**
     * 模板description
     * @access public
     * @param mixed $input 要
     * @return
     */
    public function setDescription($description = '') {
        $this->assign('_description',$description);
    }

    /**
     * 模板变量赋
     * @access protected
     * @param mixed $name 要显示的模板变量
     * @param mixed $value 变量的
     * @return void
     */
    public function assign($name,$value='') {
        if(is_array($name)) {
            $this->tVar   =  array_merge($this->tVar,$name);
        }elseif(is_object($name)){
            foreach($name as $key =>$val)
                $this->tVar[$key] = $val;
        }else {
            $this->tVar[$name] = $value;
        }
    }

    /**
     * 魔术方法：注册模版变量
     * @access protected
     * @param string $name 模版变量
     * @param mix $value 变量值
     * @return mixed
     */
    public function __set($name,$value) {
        $this->assign($name,$value);
    }

    /**
     * 取得模板显示变量的值
     * @access protected
     * @param string $name 模板显示变量
     * @return mixed
     */
    protected function get($name) {
        if(isset($this->tVar[$name]))
            return $this->tVar[$name];
        else
            return false;
    }

    /**
     * Trace变量赋值
     * @access protected
     * @param mixed $name 要显示的模板变量
     * @param mixed $value 变量的值
     * @return void
     */
    protected function trace($name,$value='') {
        if(is_array($name))
            $this->trace   =  array_merge($this->trace,$name);
        else
            $this->trace[$name] = $value;
    }

    /**
     * Ajax方式返回数据到客户端
     * @access protected
     * @param mixed $data 要返回的数据
     * @param String $info 提示信息
     * @param boolean $status 返回状态
     * @param String $status ajax返回类型 JSON XML
     * @return void
     */
    protected function ajaxReturn($data,$info='',$status=1,$type='JSON') {
        // 保证AJAX返回后也能保存日志
        $result  =  array();
        $result['status']  =  $status;
        $result['info'] =  $info;
        $result['data'] = $data;
        if(strtoupper($type)=='JSON') {
            // 返回JSON数据格式到客户端 包含状态信息
            header("Content-Type:text/html; charset=utf-8");
            exit(json_encode($result));
        }elseif(strtoupper($type)=='XML'){
            // 返回xml格式数据
            header("Content-Type:text/xml; charset=utf-8");
            exit(xml_encode($result));
        }elseif(strtoupper($type)=='EVAL'){
            // 返回可执行的js脚本
            header("Content-Type:text/html; charset=utf-8");
            exit($data);
        }else{
            // TODO 增加其它格式
        }
    }

};//类定义结束